#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/11/23 15:33
# @Author : ZhengQL

import yaml
import os
from common.Path_Handler import PathHandler

class YamlHandler:
    def __init__(self, file):
        # self.file = file

        # 获取当前脚本所在目录
        current_dir = os.path.dirname(os.path.abspath(__file__))
        # 构建配置文件的绝对路径
        self.file = os.path.join(current_dir, file)


    def read_yaml(self):
        with open(self.file, 'r', encoding='utf-8') as f:
            data = yaml.load(f, Loader=yaml.FullLoader)
        return data

    def write_yaml(self, data):
        # 读取现有的 YAML 内容
        existing_data = self.read_yaml()
        # 更新现有数据
        existing_data.update(data)
        # 将更新后的内容写回文件
        with open(self.file, 'w', encoding='utf-8') as f:
            yaml.safe_dump(existing_data, f, allow_unicode=True)


yaml_data = YamlHandler(PathHandler.config_path + r'/config.yaml').read_yaml()

if __name__ == '__main__':
    yaml_data = YamlHandler(PathHandler.config_path + r'/config.yaml').read_yaml()['login']['url']['url']
    print(f"url = {yaml_data}")
#
#
#     yaml_handler = YamlHandler('../config/test_data.yaml')
#     yaml_handler.write_yaml({'get_keyword': {'name': '12333333333'}})
